package com.xhxi.photobooker.controller;

import com.xhxi.photobooker.constant.JwtClaimsConstant;
import com.xhxi.photobooker.entity.Order;
import com.xhxi.photobooker.entity.Photographer;
import com.xhxi.photobooker.properties.JwtProperties;
import com.xhxi.photobooker.result.Result;
import com.xhxi.photobooker.service.OrderService;
import com.xhxi.photobooker.service.PhotographerService;
import com.xhxi.photobooker.service.UserService;
import com.xhxi.photobooker.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import jakarta.annotation.PostConstruct;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;
import com.alibaba.fastjson.JSON;

@RestController
@RequestMapping("/photo/photographer")

public class PhotographerController {

    @Autowired
    private PhotographerService photographerService;
    private static String jwtSecret;
    @Autowired
    private OrderService orderService;
    @Autowired
    private UserService userService;

    @Autowired
    private JwtProperties jwtProperties;

    @PostConstruct
    public void init() {
        jwtSecret = jwtProperties.getUserSecretKey();
    }

    @GetMapping("/list")
    public Result<List<Photographer>> list() {
        List<Photographer> list = photographerService.findAllPhotographer();
        return Result.success(list);
    }

    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody Photographer photographer) {
        photographerService.updatePhotographer(photographer.getId(), photographer);
        return Result.success(true);
    }
    @PostMapping("/deleteBatch")
    @Transactional
    public Result<Boolean> deleteBatch(@RequestBody List<Long> ids) {
        for (Long id : ids) {
            photographerService.deletePhotographer(id);
        }
        return Result.success(true);
    }

    @GetMapping("/orders/{photographerId}")
    public Result<List<Order>> listPhotographerOrders(@PathVariable Long photographerId) {
        List<Order> orders = orderService.listOrdersByPhotographerId(photographerId);
        for (Order order : orders) {
            com.xhxi.photobooker.entity.User user = userService.findUserById(order.getUserId());
            order.setUserName(user != null ? user.getUsername() : "未知用户");
        }
        return Result.success(orders);
    }
    @GetMapping("/{id}")
    public Result<Photographer> getPhotographerById(@PathVariable Long id) {
        Photographer photographer = photographerService.findPhotographerById(id);
        return photographer != null ? Result.success(photographer) : Result.error("摄影师不存在");
    }
    @GetMapping("/byUser/{userId}")
    public Result<Photographer> getByUserId(@PathVariable Long userId) {
        Photographer photographer = photographerService.findByUserId(userId);
        if (photographer == null) {
            return Result.success(null); // 不是摄影师
        }
        return Result.success(photographer); // 是摄影师
    }

    @GetMapping("/map")
    public Result<List<Map<String, Object>>> getPhotographerMap() {
        List<Photographer> list = photographerService.getAllPhotographers();
        List<Map<String, Object>> result = list.stream()
            .filter(p -> p.getLocationVisible() == null || p.getLocationVisible() == 1)
            .map(p -> {
                Map<String, Object> map = new HashMap<>();
                map.put("id", p.getId());
                map.put("name", p.getName());
                map.put("latitude", p.getLatitude());
                map.put("longitude", p.getLongitude());
                map.put("phone",p.getPhone());
                return map;
            }).collect(Collectors.toList());
        return Result.success(result);
    }

    //修改摄影师位置可见性
    @PostMapping("/toggle-location-visible")
    public Result<?> toggleLocationVisible(@RequestParam Long id, @RequestParam Integer visible) {
        Photographer photographer = photographerService.findPhotographerById(id);
        if (photographer == null) return Result.error("摄影师不存在");
        photographer.setLocationVisible(visible);
        photographerService.updatePhotographer(id, photographer);
        return Result.success();
    }
    //修改定位
    @PostMapping("/update-location")
    public Result<?> updateLocation(@RequestParam Long id, @RequestParam Double latitude, @RequestParam Double longitude) {
        Photographer photographer = photographerService.findByUserId(id);
        if (photographer == null) return Result.error("摄影师不存在");
        // 限制一天只能上传一次
        Date now = new Date();
        /*if (photographer.getLastLocationUpdateTime() != null) {
            long diff = now.getTime() - photographer.getLastLocationUpdateTime().getTime();
            if (diff < 24 * 60 * 60 * 1000) {
                return Result.error("一天只能上传一次位置");
            }
        }*/
        photographer.setLatitude(latitude);
        photographer.setLongitude(longitude);
        photographer.setLastLocationUpdateTime(now);
        photographerService.updatePhotographer(id, photographer);
        return Result.success();
    }


}
